package com.socket.communication;


import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @ClassName: Server
 * @Author: ZhangYue26
 * @Description: Socket实现双向通信-服务端
 * @Date: 2020-08-18
 */
public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(12345);
        Socket accept = serverSocket.accept();
        InputStream inputStream = accept.getInputStream();
        byte[] bytes = new byte[1024];
        inputStream.read(bytes);
        System.out.println(new String(bytes,"UTF-8"));

//        InputStreamReader inputStreamReader;
//        BufferedReader bufferedReader;
//        OutputStreamWriter outputStreamWriter;
//        BufferedWriter bufferedWriter;
//        String str;
//        Scanner in = new Scanner(System.in);
//        try {
//            ServerSocket server = new ServerSocket(4444);// 在本机的4444端口开放Server
//            Socket socket = server.accept();// 只要产生连接，socket便可以代表所连接的那个物体，同时这个server.accept()只有产生了连接才会进行下一步操作。
//            System.out.println(socket.getInetAddress());// 输出连接者的IP。
//            System.out.println("建立了一个连接！");
//            while (true) {
//                inputStreamReader = new InputStreamReader(socket.getInputStream());
//                bufferedReader = new BufferedReader(inputStreamReader);
//                System.out.println(socket.getInetAddress() + ":" + bufferedReader.readLine());
//                outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
//                bufferedWriter = new BufferedWriter(outputStreamWriter);
//                System.out.print("回复:");
//                str = in.nextLine();
//                bufferedWriter.write(str + "\n");
//                bufferedWriter.flush();
//            }
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }

}
